import javax.swing.*;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-05-10
 * Time: 21:24
 */
public class MyLinkedList {
    static class ListNode{
      public   int val;
      public   ListNode pre;
       public ListNode next;
       public ListNode (int val){
           this.val=val;
       }
    }
    public ListNode head;//指向第一个结点
    public ListNode last;//指向最后一个结点


  //头插法
         public void addFirst(int data){
        ListNode listNode=new ListNode(data);
            if(head==null){
                head=listNode;
                last=listNode;
            }else{
                listNode.next=head;
                head.pre=listNode;
                head=listNode;

            }
         }
 //尾插法
         public void addLast(int data){
             ListNode listNode=new ListNode(data);
             if(head==null){
                 head=listNode;
                 last=listNode;
             }else{
                 last.next=listNode;
                 listNode.pre=last;
                 last=listNode;
             }
         }
         public ListNode find(int index){
            ListNode cur=this.head;
            while(index>0){
                cur=cur.next;
                index--;
            }
            return cur;
         }
         public int len(){
             ListNode cur=this.head;
             int l=0;
             while(cur!=null){
                 cur=cur.next;
                 l++;
             }
             return l;
         }
  //任意位置插入,第一个数据节点为0号下标
         public boolean addIndex(int index,int data){
            int l=len();
            if(index<0||index>l){
                return false;
            }
            if(index==0){
                addFirst(data);
                return true;
            }else if(index==l){
                addLast(data);
                return true;
            }
            else{
                ListNode cur=find(index);
                ListNode insert=new ListNode(data);
                cur.pre.next=insert;
                insert.pre=cur.pre;
                insert.next=cur;
                cur.pre=insert;
                return true;
            }

         }
  //查找是否包含关键字key是否在单链表当中
          public boolean contains(int key){
             ListNode cur=this.head;
             while(cur!=null){
                 if(cur.val==key){
                     return true;
                 }
                 cur=cur.next;
             }
        return false;
          }
  //删除第一次出现关键字为key的节点
          public void remove(int key){
            ListNode cur=this.head;
            while(cur!=null){
                if(cur.val==key){
                    if(cur==this.head){
                      this.head=this.head.next;
                      head.pre=null;
                    }else if(cur==this.last){
                        last=last.pre;
                        last.next=null;
                    }else{
                        cur.pre.next=cur.next;
                        cur.next.pre=cur.pre;
                    }
                    return ;
                }
                else{
                    cur=cur.next;
                }

            }
          }
  //删除所有值为key的节点
          public void removeAllKey(int key){
              ListNode cur=this.head;
              while(cur!=null){
                  if(cur.val==key){
                      if(cur==this.head){
                          this.head=this.head.next;
                          head.pre=null;
                      }else if(cur==this.last){
                          last=last.pre;
                          last.next=null;
                      }else{
                          cur.pre.next=cur.next;
                          cur.next.pre=cur.pre;
                      }

                  }

                      cur=cur.next;


              }
          }


  public void display(){
        ListNode cur=this.head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
      System.out.println();
  }
  public void clear(){
        ListNode cur=this.head;
        while(cur!=null){
            ListNode curNext=cur.next;
            cur.pre=null;
            cur.next=null;
            cur=curNext;

        }
  }

    }
